local awesome, client, mouse, screen, tag = awesome, client, mouse, screen, tag
local ipairs, string, os, table, tostring, tonumber, type = ipairs, string, os, table, tostring, tonumber, type

-- awesome标准库
local gears, awful, wibox = require("gears"), require("awful"), require("wibox")
-- local gears, awful, wibox, vicious = require("gears"), require("awful"), require("wibox"), require("vicious")
require("awful.autofocus")
local beautiful, naughty, menubar = require("beautiful"), require("naughty"), require("menubar")

-- local hotkeys_popup = require("awful.hotkeys_popup")
-- Enable hotkeys help widget for VIM and other apps when client with a matching name is opened:
-- require("awful.hotkeys_popup.keys")

-- awesome启动错误
if awesome.startup_errors then
    naughty.notify(
        {
            preset = naughty.config.presets.critical,
            title = "Oops, there were errors during startup!",
            text = awesome.startup_errors
        }
    )
end

-- awesome运行错误
do
    local in_error = false
    awesome.connect_signal(
        "debug::error",
        function(err)
            if in_error then -- Make sure we don't go into an endless error loop
                return
            end
            in_error = true

            naughty.notify(
                {
                    preset = naughty.config.presets.critical,
                    title = "Oops, an error happened!",
                    text = tostring(err)
                }
            )
            in_error = false
        end
    )
end

-- 导入theme.lua
beautiful.init(awful.util.getdir("config") .. "/themes/avanti/theme.lua")

-- 默认的 终端 编辑器 浏览器 文件管理器 mod键
terminal, editor, browser, filemanager, modkey = "urxvt", "code", "chromium", "pcmanfm", "Mod4"
editor_cmd = terminal .. " -e " .. editor

-- urxvt cool-retro-term

-- awful.util.tagnames = {"钟", "剑", "斧", "壶", "塔", "琴", "鼎", "印", "镜", "石"}
awful.util.tagnames = {
    "🀐",
    "🀑",
    "🀒",
    "🀓",
    "🀔",
    "🀕",
    "🀖",
    "🀗",
    "🀘",
    "🀙",
    "🀚",
    "🀛",
    "🀜",
    "🀝",
    "🀞",
    "🀟",
    "🀠",
    "🀡",
    "🀇",
    "🀈",
    "🀉",
    "🀊",
    "🀋",
    "🀌",
    "🀍",
    "🀎",
    "🀏",
    "🀀",
    "🀁",
    "🀂",
    "🀃",
    "🀄",
    "🀅",
    "🀆",
    "🀀"
}

-- awesome布局
awful.layout.layouts = {awful.layout.suit.corner.nw, awful.layout.suit.max, awful.layout.suit.floating}

-- 输入法菜单中的子菜单
myinputmenu = {
    {"英  文", "fcitx5-remote -s keyboard-us"},
    {"中  文", "fcitx5-remote -s pinyin"},
    {"日  文", "fcitx5-remote -s mozc"}
}

-- wifi菜单中的子菜单
mywifimenu = {
    {"关  闭", "nmcli device disconnect wlp59s0"},
    {"热  点", "nmcli connection up Hotspot"}
}

-- awesome菜单中的子菜单
myawesomemenu = {
    {"配  置", string.format("%s %s", editor, awesome.conffile)},
    {"主  题", string.format("%s %s/.config/awesome/themes/avanti/theme.lua", editor, os.getenv("HOME"))},
    {"启  动", string.format("%s %s/.config/awesome/autorun.sh", editor, os.getenv("HOME"))},
    {"重  启", awesome.restart},
    {
        "退  出",
        function()
            awesome.quit()
        end
    }
}

-- conky菜单中的子菜单
myconkymenu = {
    {"编  辑", string.format("%s %s/.config/conky/conky_avanti.lua", editor, os.getenv("HOME"))},
    {"编  辑", string.format("%s %s/.config/conky/conkyrc", editor, os.getenv("HOME"))},
    {"启  动", string.format("conky -c %s/.config/conky/conkyrc", os.getenv("HOME"))},
    {"退  出", "killall conky"}
}

-- 退出菜单中的子菜单
myexitmenu = {
    {
        "登  出",
        function()
            awesome.quit()
        end
    },
    {"待  机", "systemctl suspend"},
    {"休  眠", "systemctl hibernate"},
    {"重  启", "systemctl reboot"},
    {"关  机", "poweroff"}
}

mymainmenu =
    awful.menu(
    {
        items = {
            {"终  端", terminal},
            {"网  络", "/usr/bin/chromium --disable-gpu"},
            {"编  辑", "/usr/bin/code --disable-gpu"}, -- /usr/bin/code --disable-gpu
            {"文  件", filemanager},
            {"写  作", "/usr/bin/typora"},
            {"微  信", "/usr/bin/electronic-wechat --disable-gpu"},
            {"音  乐", "/usr/bin/audacious"},
            {"下  载", "/usr/bin/transmission-qt"},
            {"输  入", myinputmenu},
            {"无  线", mywifimenu},
            {"桌  面", myawesomemenu},
            {"监  视", myconkymenu},
            {"退  出", myexitmenu}
        }
    }
)

-- menubar.utils.terminal = terminal -- Set the terminal for applications that require it

mykeyboardlayout = awful.widget.keyboardlayout()

-- taglist上鼠标动作定义 1:左键单击 3:右键单击 4:中键上滚 5:中键下滚
-- 所有快捷键都在gears.table中
local taglist_buttons =
    gears.table.join(
    awful.button( -- 左键单击 查看这个tag
        {},
        1,
        function(t)
            t:view_only()
        end
    ),
    awful.button( -- 右键单击 将当前这个client移到点击的tag中
        {},
        3,
        function(t)
            if client.focus then
                client.focus:move_to_tag(t)
            end
        end
    ),
    awful.button( -- 中键上滚 切换到下一个tag
        {},
        4,
        function(t)
            awful.tag.viewnext(t.screen)
        end
    ),
    awful.button( -- 中键下滚 切换到上一个tag
        {},
        5,
        function(t)
            awful.tag.viewprev(t.screen)
        end
    )
)

-- tasklist上鼠标动作定义 1:左键单击 3:右键单击 4:中键上滚 5:中键下滚
local tasklist_buttons =
    gears.table.join(
    awful.button( -- 左键单击 最小化当前client
        {},
        1,
        function(c)
            if c == client.focus then
                c.minimized = true
            else
                c:emit_signal("request::activate", "tasklist", {raise = true})
            end
        end
    ),
    awful.button( -- 右键单击 显示所有正在运行的client
        {},
        3,
        function()
            awful.menu.client_list({theme = {width = 250}})
        end
    ),
    awful.button( -- 中键上滚 切换到下一个client
        {},
        4,
        function()
            awful.client.focus.byidx(1)
        end
    ),
    awful.button( -- 中键下滚 切换到上一个client
        {},
        5,
        function()
            awful.client.focus.byidx(-1)
        end
    )
)

local function set_wallpaper(s)
    if beautiful.wallpaper then
        local wallpaper = beautiful.wallpaper
        if type(wallpaper) == "function" then -- 如果wallpaper是一个函数
            wallpaper = wallpaper(s)
        end
        gears.wallpaper.maximized(wallpaper, s)
    end
end

screen.connect_signal("property::geometry", set_wallpaper) -- 当一个屏的分辨率改变时 壁纸也跟着调整

-- 设置cpu的widget
cpubar =
    wibox.widget {
    border_color = beautiful.fg_normal,
    bg = "#DDDDFF30",
    color = beautiful.fg_normal,
    border_width = 1,
    forced_width = 48,
    forced_height = 48,
    min_value = 0,
    max_value = 1,
    thickness = 5,
    widget = wibox.container.arcchart
}
cpuwidget_text = awful.tooltip({objects = {cpubar}})
cpuinfo = {0, 0, 0, 0, 0, 0, 0}
awful.widget.watch(
    [[bash -c ""]],
    1,
    function(widget, stdout)
        i = 0
        for line in io.lines("/proc/stat") do
            if i == 0 then
                j = 0
                cputotal = 0
                cpuidle = 0
                for word in string.gmatch(line, "%d+") do
                    j = j + 1
                    if j <= 7 then
                        if cpuinfo[j] == 0 then
                            cpuinfo[j] = word
                        else
                            cputotal = cputotal + word - cpuinfo[j]
                            if j == 4 then
                                cpuidle = cpuidle + word - cpuinfo[j]
                            end
                            cpuinfo[j] = word
                        end
                    else
                        break
                    end
                end
                break
            end
        end
        if cputotal > 0 then
            used_perc = 1 - cpuidle / cputotal
        else
            used_perc = 0
        end
        widget.value = used_perc
        cpuwidget_text:set_text(string.format("处理器总使用率：%s%%", math.floor(used_perc * 100)))
    end,
    cpubar
)
local cpuwidget = wibox.container.margin(wibox.container.mirror(cpubar, {horizontal = true}), 0, 10, 4, 4)

--[[ 
cpuwidget =
    wibox.widget {
    forced_width = 100,
    border_width = 1,
    border_color = beautiful.fg_normal,
    color = beautiful.fg_normal,
    background_color = beautiful.bg_normal,
    widget = wibox.widget.graph
}
cpuwidget_text = awful.tooltip({objects = {cpuwidget}})
vicious.register(
    cpuwidget,
    vicious.widgets.cpu,
    function(widget, args)
        cpuwidget_text:set_text(
            string.format(
                "处理器总使用率：%s%%\n核心使用率：%s%%, %s%%, %s%%, %s%%, %s%%, %s%%, %s%%, %s%%, %s%%, %s%%, %s%%, %s%%, %s%%, %s%%, %s%%, %s%%",
                args[1],
                args[2],
                args[3],
                args[4],
                args[5],
                args[6],
                args[7],
                args[8],
                args[9],
                args[10],
                args[11],
                args[12],
                args[13],
                args[14],
                args[15],
                args[16],
                args[17]
            )
        )
        return args[1]
    end,
    1
)
local cpuwidget = wibox.container.margin(wibox.container.mirror(cpuwidget, {horizontal = true}), 5, 10, 4, 4) -- 镜像反转 从右向左走 同时控制间隔
--]]
-- 设置内存的widget
membar =
    wibox.widget {
    border_color = beautiful.fg_normal,
    bg = "#DDDDFF30",
    color = beautiful.fg_normal,
    border_width = 1,
    forced_width = 48,
    forced_height = 48,
    min_value = 0,
    max_value = 1,
    thickness = 5,
    widget = wibox.container.arcchart
}
memwidget_text = awful.tooltip({objects = {membar}})
awful.widget.watch(
    [[bash -c ""]],
    10,
    function(widget, stdout)
        mem_now = {}
        for line in io.lines("/proc/meminfo") do
            for k, v in string.gmatch(line, "([%a]+):[%s]+([%d]+).+") do
                if k == "MemTotal" then
                    mem_now.total = v
                elseif k == "MemAvailable" then
                    mem_now.ava = v
                end
            end
        end

        mem_now.used = mem_now.total - mem_now.ava
        used_perc = mem_now.used / mem_now.total
        widget.value = used_perc
        memwidget_text:set_text(
            string.format("内存消耗：%sMiB / %sMiB", math.floor(mem_now.used / 1024), math.floor(mem_now.total / 1024))
        )
    end,
    membar
)
local memwidget = wibox.container.margin(wibox.container.mirror(membar, {horizontal = true}), 0, 10, 4, 4)

-- 设置硬盘的widget
diskrootbar =
    wibox.widget {
    border_color = beautiful.fg_normal,
    bg = "#DDDDFF30",
    color = beautiful.fg_normal,
    border_width = 1,
    forced_width = 48,
    forced_height = 48,
    min_value = 0,
    max_value = 1,
    thickness = 5,
    widget = wibox.container.arcchart
}
diskrootwidget_text = awful.tooltip({objects = {diskrootbar}})
local Gio = require("lgi").Gio
local query_size = Gio.FILE_ATTRIBUTE_FILESYSTEM_SIZE
local query_free = Gio.FILE_ATTRIBUTE_FILESYSTEM_FREE
local query_used = Gio.FILE_ATTRIBUTE_FILESYSTEM_USED
local query = query_size .. "," .. query_free .. "," .. query_used
awful.widget.watch(
    [[bash -c ""]],
    10,
    function(widget, stdout)
        for _, mount in ipairs(Gio.unix_mounts_get()) do
            local path = Gio.unix_mount_get_mount_path(mount)
            local root = Gio.File.new_for_path(path)
            local info = root:query_filesystem_info(query)
            if path == "/" then
                if info then
                    local size = info:get_attribute_uint64(query_size)
                    local used = info:get_attribute_uint64(query_used)
                    local free = info:get_attribute_uint64(query_free)
                    widget.value = used / size
                    diskrootwidget_text:set_text(
                        string.format(
                            "ROOT：%sGiB / %sGiB",
                            math.floor(10 * (size - free) / 1024 ^ 3) / 10,
                            math.floor(10 * size / 1024 ^ 3) / 10
                        )
                    )
                end
            end
        end
    end,
    diskrootbar
)
local diskrootwidget = wibox.container.margin(wibox.container.mirror(diskrootbar, {horizontal = true}), 0, 4, 4, 4)

diskhomebar =
    wibox.widget {
    border_color = beautiful.fg_normal,
    bg = "#DDDDFF30",
    color = beautiful.fg_normal,
    border_width = 1,
    forced_width = 48,
    forced_height = 48,
    min_value = 0,
    max_value = 1,
    thickness = 5,
    widget = wibox.container.arcchart
}
diskhomewidget_text = awful.tooltip({objects = {diskhomebar}})
local Gio = require("lgi").Gio
local query_size = Gio.FILE_ATTRIBUTE_FILESYSTEM_SIZE
local query_free = Gio.FILE_ATTRIBUTE_FILESYSTEM_FREE
local query_used = Gio.FILE_ATTRIBUTE_FILESYSTEM_USED
local query = query_size .. "," .. query_free .. "," .. query_used
awful.widget.watch(
    [[bash -c ""]],
    10,
    function(widget, stdout)
        for _, mount in ipairs(Gio.unix_mounts_get()) do
            local path = Gio.unix_mount_get_mount_path(mount)
            local root = Gio.File.new_for_path(path)
            local info = root:query_filesystem_info(query)
            if path == "/home" then
                if info then
                    local size = info:get_attribute_uint64(query_size)
                    local used = info:get_attribute_uint64(query_used)
                    local free = info:get_attribute_uint64(query_free)
                    widget.value = used / size
                    diskhomewidget_text:set_text(
                        string.format(
                            "HOME：%sGiB / %sGiB",
                            math.floor(10 * (size - free) / 1024 ^ 3) / 10,
                            math.floor(10 * size / 1024 ^ 3) / 10
                        )
                    )
                end
            end
        end
    end,
    diskhomebar
)
local diskhomewidget = wibox.container.margin(wibox.container.mirror(diskhomebar, {horizontal = true}), 0, 10, 4, 4)

if false then
    -- 设置网络的widget
    netdownbar =
        wibox.widget {
        border_color = beautiful.fg_normal,
        bg = "#DDDDFF30",
        color = beautiful.fg_normal,
        border_width = 1,
        forced_width = 48,
        forced_height = 48,
        min_value = 0,
        max_value = 1,
        thickness = 5,
        widget = wibox.container.arcchart
    }
    netdownwidget_text = awful.tooltip({objects = {netdownbar}})
    awful.widget.watch(
        [[bash -c "sar -n DEV 1 1 | grep ens20u1u1 | grep 平均时间 | awk '{print $5}'"]],
        1,
        function(widget, stdout)
            netdownwidget_text:set_text(string.format("有线下载：%sKB", stdout:gsub("\n", "")))
            local value = tonumber(stdout)
            if value >= 1024 then
                widget.value = 1
            else
                widget.value = value / 1024
            end
        end,
        netdownbar
    )
    local netdownwidget = wibox.container.margin(wibox.container.mirror(netdownbar, {horizontal = true}), 0, 4, 4, 4)

    netupbar =
        wibox.widget {
        border_color = beautiful.fg_normal,
        bg = "#DDDDFF30",
        color = beautiful.fg_normal,
        border_width = 1,
        forced_width = 48,
        forced_height = 48,
        min_value = 0,
        max_value = 1,
        thickness = 5,
        widget = wibox.container.arcchart
    }
    netupwidget_text = awful.tooltip({objects = {netupbar}})
    awful.widget.watch(
        [[bash -c "sar -n DEV 1 1 | grep ens20u1u1 | grep 平均时间 | awk '{print $6}'"]],
        1,
        function(widget, stdout)
            netupwidget_text:set_text(string.format("有线上传：%sKB", stdout:gsub("\n", "")))
            local value = tonumber(stdout)
            if value >= 1024 then
                widget.value = 1
            else
                widget.value = value / 1024
            end
        end,
        netupbar
    )
    local netupwidget = wibox.container.margin(wibox.container.mirror(netupbar, {horizontal = true}), 0, 4, 4, 4)

    wifidownbar =
        wibox.widget {
        border_color = beautiful.fg_normal,
        bg = "#DDDDFF30",
        color = beautiful.fg_normal,
        border_width = 1,
        forced_width = 48,
        forced_height = 48,
        min_value = 0,
        max_value = 1,
        thickness = 5,
        widget = wibox.container.arcchart
    }
    wifidownwidget_text = awful.tooltip({objects = {wifidownbar}})
    awful.widget.watch(
        [[bash -c "sar -n DEV 1 1 | grep wlp59s0 | grep 平均时间 | awk '{print $5}'"]],
        1,
        function(widget, stdout)
            wifidownwidget_text:set_text(string.format("无线下载：%sKB", stdout:gsub("\n", "")))
            local value = tonumber(stdout)
            if value >= 1024 then
                widget.value = 1
            else
                widget.value = value / 1024
            end
        end,
        wifidownbar
    )
    local wifidownwidget = wibox.container.margin(wibox.container.mirror(wifidownbar, {horizontal = true}), 0, 4, 4, 4)

    wifiupbar =
        wibox.widget {
        border_color = beautiful.fg_normal,
        bg = "#DDDDFF30",
        color = beautiful.fg_normal,
        border_width = 1,
        forced_width = 48,
        forced_height = 48,
        min_value = 0,
        max_value = 1,
        thickness = 5,
        widget = wibox.container.arcchart
    }
    wifiupwidget_text = awful.tooltip({objects = {wifiupbar}})
    awful.widget.watch(
        [[bash -c "sar -n DEV 1 1 | grep wlp59s0 | grep 平均时间 | awk '{print $6}'"]],
        1,
        function(widget, stdout)
            wifiupwidget_text:set_text(string.format("无线上传：%sKB", stdout:gsub("\n", "")))
            local value = tonumber(stdout)
            if value >= 1024 then
                widget.value = 1
            else
                widget.value = value / 1024
            end
        end,
        wifiupbar
    )
    local wifiupwidget = wibox.container.margin(wibox.container.mirror(wifiupbar, {horizontal = true}), 0, 10, 4, 4)
end

-- 设置电池的widget
function first_line(path)
    local file, first = io.open(path, "rb"), nil
    if file then
        first = file:read("*l")
        file:close()
    end
    return first
end
local batbar =
    wibox.widget {
    border_color = beautiful.fg_normal,
    bg = "#DDDDFF30",
    color = beautiful.fg_normal,
    border_width = 1,
    forced_width = 48,
    forced_height = 48,
    min_value = 0,
    max_value = 1,
    thickness = 5,
    widget = wibox.container.arcchart
}
batwidget_text = awful.tooltip({objects = {batbar}})
awful.widget.watch(
    [[bash -c ""]],
    10,
    function(widget, stdout)
        local charge_full = first_line("/sys/class/power_supply/BAT0/charge_full")
        local charge_now = first_line("/sys/class/power_supply/BAT0/charge_now")
        widget.value = charge_now / charge_full
        batwidget_text:set_text(string.format("电量：%s%%", math.floor(100 * charge_now / charge_full)))
    end,
    batbar
)
local batwidget = wibox.container.margin(wibox.container.mirror(batbar, {horizontal = true}), 0, 10, 4, 4)

-- 设置温度的widget
tempbar =
    wibox.widget {
    border_color = beautiful.fg_normal,
    bg = "#DDDDFF30",
    color = beautiful.fg_normal,
    border_width = 1,
    forced_width = 48,
    forced_height = 48,
    min_value = 0,
    max_value = 100,
    thickness = 5,
    widget = wibox.container.arcchart
}
tempwidget_text = awful.tooltip({objects = {tempbar}})
local hwmon = io.popen("ls /sys/devices/platform/coretemp.0/hwmon/ | sed 's/.*hwmon(*)/\1/g'"):read("*all")
local tempfile =
    "/sys/devices/platform/coretemp.0/hwmon/hwmon" ..
    string.char(string.byte(string.sub(hwmon, -2, -1))) .. "/temp1_input"
awful.widget.watch(
    [[bash -c ""]],
    5,
    function(widget, stdout)
        local f = io.open(tempfile)
        if f then
            local value = tonumber(f:read("*all")) / 1000
            widget.value = value
            tempwidget_text:set_text(string.format("温度：%s°C", value))
            f:close()
        end
    end,
    tempbar
)
local tempwidget = wibox.container.margin(wibox.container.mirror(tempbar, {horizontal = true}), 0, 10, 4, 4)

-- 设置亮度的widget
local GET_BRIGHTNESS_CMD = "xbacklight -get"
local INC_BRIGHTNESS_CMD = "xbacklight -inc 10"
local DEC_BRIGHTNESS_CMD = "xbacklight -dec 10"
local TOG_BRIGHTNESS_CMD = "xbacklight -set 100"

local brightnessbar =
    wibox.widget {
    border_color = beautiful.fg_normal,
    bg = "#DDDDFF30",
    color = beautiful.fg_normal,
    border_width = 1,
    forced_width = 48,
    forced_height = 48,
    min_value = 0,
    max_value = 100,
    thickness = 5,
    widget = wibox.container.arcchart
}

local brightnesswidget_text = awful.tooltip({objects = {brightnessbar}})

local update_brightness = function(widget, stdout, _, _, _)
    local volume = tonumber(stdout)
    widget.value = volume
    volume = math.floor(volume)
    brightnesswidget_text:set_text(string.format("亮度：%d", volume))
end

brightnessbar:connect_signal(
    "button::press",
    function(_, _, _, button)
        if (button == 4) then
            awful.spawn(INC_BRIGHTNESS_CMD)
        elseif (button == 5) then
            awful.spawn(DEC_BRIGHTNESS_CMD)
        elseif (button == 1) then
            awful.spawn(TOG_BRIGHTNESS_CMD)
        end

        awful.spawn.easy_async(
            GET_BRIGHTNESS_CMD,
            function(stdout, stderr, exitreason, exitcode)
                update_graphic(brightnessbar, stdout, stderr, exitreason, exitcode)
            end
        )
    end
)
awful.widget.watch(GET_BRIGHTNESS_CMD, 1, update_brightness, brightnessbar)
local brightnesswidget = wibox.container.margin(wibox.container.mirror(brightnessbar, {horizontal = true}), 0, 10, 4, 4)

-- 设置声音的widget
local GET_VOLUME_CMD = "amixer"
local INC_VOLUME_CMD = "amixer set Master 5%+"
local DEC_VOLUME_CMD = "amixer set Master 5%-"
local TOG_VOLUME_CMD = "amixer set Master mute" -- 静音

local volumebar =
    wibox.widget {
    border_color = beautiful.fg_normal,
    bg = "#DDDDFF30",
    color = beautiful.fg_normal,
    border_width = 1,
    forced_width = 48,
    forced_height = 48,
    min_value = 0,
    max_value = 100,
    thickness = 5,
    widget = wibox.container.arcchart
}

local volumewidget_text = awful.tooltip({objects = {volumebar}})

local update_volume = function(widget, stdout, _, _, _)
    local volume = string.match(stdout, "%d+%%"):gsub("%%", "")
    widget.value = volume
    volumewidget_text:set_text(string.format("声音：%d", volume))
end

volumebar:connect_signal(
    "button::press",
    function(_, _, _, button)
        if (button == 4) then
            awful.spawn(INC_VOLUME_CMD)
        elseif (button == 5) then
            awful.spawn(DEC_VOLUME_CMD)
        elseif (button == 1) then
            awful.spawn(TOG_VOLUME_CMD)
        end

        awful.spawn.easy_async(
            GET_VOLUME_CMD,
            function(stdout, stderr, exitreason, exitcode)
                update_graphic(volumewidget, stdout, stderr, exitreason, exitcode)
            end
        )
    end
)
awful.widget.watch(GET_VOLUME_CMD, 1, update_volume, volumebar)
local volumewidget = wibox.container.margin(wibox.container.mirror(volumebar, {horizontal = true}), 0, 10, 4, 4)

-- day_name = {"🌙", "🔥", "💧", "🌳", "🥇", "🌾", "☀"}
day_name = {"月", "火", "水", "木", "金", "土", "日"}
big_name = {"壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖", "拾", "廿", "卅"}

function to_large(num)
    if num <= 10 then
        return big_name[num]
    elseif num < 20 then
        return big_name[10] .. big_name[num - 10]
    elseif num == 20 then
        return big_name[11]
    elseif num < 30 then
        return big_name[11] .. big_name[num - 20]
    elseif num == 30 then
        return big_name[12]
    else
        return big_name[12] .. big_name[num - 30]
    end
end

-- 设置日期
local date =
    awful.widget.watch(
    "date +'%m%d'",
    60,
    function(widget, stdout)
        widget:set_markup(
            to_large(tonumber(string.sub(stdout, 1, 2))) .. "-" .. to_large(tonumber(string.sub(stdout, 3, 4)))
        )
        widget:set_font(beautiful.font)
    end
)
local datewidget = wibox.container.margin(date, 5, 5, 2, 2)

-- 设置星期
local wday =
    awful.widget.watch(
    "date +'%w'",
    60,
    function(widget, stdout)
        local w = tonumber(stdout)
        if w == 0 then -- date + %w 返回的是0~6 0表示周日
            w = w + 7
        end
        widget:set_markup(day_name[w])
        widget:set_font(beautiful.font)
    end
)
local wdaywidget = wibox.container.margin(wday, 5, 5, 2, 2)

-- 设置时间
local clock =
    awful.widget.watch(
    "date +'%R'",
    1,
    function(widget, stdout)
        widget:set_markup(stdout)
        widget:set_font("OperatorFZLongZhao 18")
    end
)
local clockwidget = wibox.container.margin(clock, 5, 10, 2, 2)

screen_count = 0
awful.screen.connect_for_each_screen( -- 每一个屏上都执行如下代码
    function(s)
        set_wallpaper(s) -- 设置壁纸
        awful.tag.add(
            awful.util.tagnames[screen_count * 9 + 1],
            {
                layout = awful.layout.layouts[1],
                master_fill_policy = "master_width_factor",
                gap_single_client = true,
                gap = 4,
                screen = s,
                selected = true
            }
        )
        awful.tag.add(
            awful.util.tagnames[screen_count * 9 + 2],
            {
                layout = awful.layout.layouts[2],
                master_fill_policy = "master_width_factor",
                gap_single_client = true,
                gap = 0,
                screen = s
            }
        )
        awful.tag.add(
            awful.util.tagnames[screen_count * 9 + 3],
            {
                layout = awful.layout.layouts[2],
                master_fill_policy = "master_width_factor",
                gap_single_client = true,
                gap = 0,
                screen = s
            }
        )
        awful.tag.add(
            awful.util.tagnames[screen_count * 9 + 4],
            {
                layout = awful.layout.layouts[2],
                master_fill_policy = "master_width_factor",
                gap_single_client = true,
                gap = 0,
                screen = s
            }
        )
        awful.tag.add(
            awful.util.tagnames[screen_count * 9 + 5],
            {
                layout = awful.layout.layouts[2],
                master_fill_policy = "master_width_factor",
                gap_single_client = true,
                gap = 0,
                screen = s
            }
        )
        awful.tag.add(
            awful.util.tagnames[screen_count * 9 + 6],
            {
                layout = awful.layout.layouts[2],
                master_fill_policy = "master_width_factor",
                gap_single_client = true,
                gap = 0,
                screen = s
            }
        )
        awful.tag.add(
            awful.util.tagnames[screen_count * 9 + 7],
            {
                layout = awful.layout.layouts[1],
                master_fill_policy = "master_width_factor",
                gap_single_client = true,
                gap = 0,
                screen = s
            }
        )
        awful.tag.add(
            awful.util.tagnames[screen_count * 9 + 8],
            {
                layout = awful.layout.layouts[1],
                master_fill_policy = "master_width_factor",
                gap_single_client = true,
                gap = 0,
                screen = s
            }
        )
        awful.tag.add(
            awful.util.tagnames[screen_count * 9 + 9],
            {
                layout = awful.layout.layouts[2],
                master_fill_policy = "master_width_factor",
                gap_single_client = true,
                gap = 0,
                screen = s
            }
        )
        screen_count = screen_count + 1
        s.mypromptbox = awful.widget.prompt()
        s.mytaglist =
            awful.widget.taglist {
            screen = s,
            filter = awful.widget.taglist.filter.all,
            buttons = taglist_buttons,
            style = {
                font = "OperatorFZLongZhao 20"
            },
            widget_template = {
                {
                    {
                        {
                            {
                                id = "icon_role",
                                widget = wibox.widget.imagebox
                            },
                            margins = 2,
                            widget = wibox.container.margin -- 控制图标的间隔
                        },
                        {
                            id = "text_role",
                            widget = wibox.widget.textbox
                        },
                        layout = wibox.layout.fixed.horizontal
                    },
                    top = -2,
                    bottom = 2,
                    widget = wibox.container.margin -- 控制文本的间隔
                },
                id = "background_role",
                widget = wibox.container.background
            }
        }
        s.mytasklist =
            wibox.container.margin(
            awful.widget.tasklist {
                screen = s,
                filter = awful.widget.tasklist.filter.currenttags,
                buttons = tasklist_buttons,
                style = {
                    font = "OperatorFZLongZhao 16"
                },
                layout = {
                    spacing = 20,
                    layout = wibox.layout.flex.horizontal
                },
                widget_template = {
                    {
                        {
                            {
                                {
                                    id = "icon_role",
                                    widget = wibox.widget.imagebox
                                },
                                margins = 2,
                                widget = wibox.container.margin -- 控制图标的间隔
                            },
                            {
                                id = "text_role",
                                widget = wibox.widget.textbox
                            },
                            layout = wibox.layout.fixed.horizontal
                        },
                        left = 10,
                        right = 10,
                        widget = wibox.container.margin -- 控制文本的间隔
                    },
                    id = "background_role",
                    widget = wibox.container.background
                }
            },
            10,
            10,
            2,
            2
        )
        s.mywibox = awful.wibar({position = "top", height = 54, screen = s})
        s.mywibox:setup {
            layout = wibox.layout.align.horizontal, -- 水平方向布局
            {layout = wibox.layout.fixed.horizontal, s.mytaglist, s.mypromptbox}, -- 左
            s.mytasklist, -- 中
            {
                layout = wibox.layout.fixed.horizontal,
                wibox.widget.textbox("核"),
                cpuwidget,
                wibox.widget.textbox("存"),
                memwidget,
                wibox.widget.textbox("磁"),
                diskrootwidget,
                diskhomewidget,
                -- wibox.widget.textbox("网"),
                -- netdownwidget,
                -- netupwidget,
                -- wifidownwidget,
                -- wifiupwidget,
                wibox.widget.textbox("电"),
                batwidget,
                wibox.widget.textbox("温"),
                tempwidget,
                -- wibox.widget.textbox("亮"),
                -- brightnesswidget,
                wibox.widget.textbox("声"),
                volumewidget,
                -- wibox.widget.systray(),
                datewidget,
                wdaywidget,
                clockwidget
            } -- 右
        }
    end
)

-- 右键调出主菜单
root.buttons(
    gears.table.join(
        awful.button(
            {},
            3,
            function()
                mymainmenu:toggle()
            end
        ),
        awful.button({}, 4, awful.tag.viewnext),
        awful.button({}, 5, awful.tag.viewprev)
    )
)

-- 全局快捷键
globalkeys =
    gears.table.join(
    awful.key({modkey}, "Left", awful.tag.viewprev), -- mod + 左键 切换至上一个tag 与鼠标中键上轮作用相同
    awful.key({modkey}, "Right", awful.tag.viewnext), -- mod + 右键 切换至下一个tag 与鼠标中键下滚作用相同
    awful.key({modkey}, "Escape", awful.tag.history.restore), -- mod + esc 回到上一个tag
    awful.key( -- mod + j 切换到下一个client
        {modkey},
        "j",
        function()
            awful.client.focus.byidx(1)
        end,
        {description = "focus next by index", group = "client"}
    ),
    awful.key( -- mod + k 切换到上一个client
        {modkey},
        "k",
        function()
            awful.client.focus.byidx(-1)
        end,
        {description = "focus previous by index", group = "client"}
    ),
    awful.key( -- mod + w 显示主菜单 跟鼠标右键作用相同
        {modkey},
        "w",
        function()
            mymainmenu:show()
        end,
        {description = "show main menu", group = "awesome"}
    ),
    awful.key( -- mod + shift + j 和下一个client交换
        {modkey, "Shift"},
        "j",
        function()
            awful.client.swap.byidx(1)
        end,
        {description = "swap with next client by index", group = "client"}
    ),
    awful.key( -- mod + shift + k 和上一个client交换
        {modkey, "Shift"},
        "k",
        function()
            awful.client.swap.byidx(-1)
        end,
        {description = "swap with previous client by index", group = "client"}
    ),
    awful.key( -- mod + shift + o 焦点移到下一个屏幕
        {modkey, "Shift"},
        "o",
        function()
            awful.screen.focus_relative(1)
        end,
        {description = "focus the next screen", group = "screen"}
    ),
    awful.key( -- mod + ctrl + o 焦点移到上一个屏幕
        {modkey, "Control"},
        "o",
        function()
            awful.screen.focus_relative(-1)
        end,
        {description = "focus the previous screen", group = "screen"}
    ),
    awful.key({modkey}, "u", awful.client.urgent.jumpto), -- mod + u 前往urgent的tag
    awful.key( -- mod + 回车 调出终端
        {modkey},
        "Return",
        function()
            awful.spawn(terminal)
        end,
        {description = "open a terminal", group = "launcher"}
    ),
    awful.key({modkey, "Shift"}, "r", awesome.restart), -- mod + ctrl + r 重启awesome
    awful.key({modkey, "Shift"}, "q", awesome.quit), -- mod + shift + q 退出awesome
    awful.key( -- mod + l 增大主终端
        {modkey},
        "l",
        function()
            awful.tag.incmwfact(0.05)
        end,
        {description = "increase master width factor", group = "layout"}
    ),
    awful.key( -- mod + h 减小主终端
        {modkey},
        "h",
        function()
            awful.tag.incmwfact(-0.05)
        end,
        {description = "decrease master width factor", group = "layout"}
    ),
    awful.key( -- mod + space 切换至下一个布局
        {modkey},
        "space",
        function()
            awful.layout.inc(1)
        end,
        {description = "select next", group = "layout"}
    ),
    awful.key( -- mod + shift + space 切换至上一个布局
        {modkey, "Shift"},
        "space",
        function()
            awful.layout.inc(-1)
        end,
        {description = "select previous", group = "layout"}
    ),
    awful.key( -- mod + x 运行lua脚本 不会用
        {modkey},
        "x",
        function()
            awful.prompt.run {
                prompt = "Run Lua code: ",
                textbox = awful.screen.focused().mypromptbox.widget,
                exe_callback = awful.util.eval,
                history_path = awful.util.get_cache_dir() .. "/history_eval"
            }
        end,
        {description = "lua execute prompt", group = "awesome"}
    ),
    -- 以下为我添加
    awful.key( -- mod + p 深度截图
        {modkey},
        "p",
        function()
            awful.spawn("/usr/bin/deepin-screen-recorder")
        end
    ),
    awful.key( -- mod + r 启动rofi
        {modkey},
        "r",
        function()
            awful.spawn("/usr/bin/rofi -show drun -modi drun")
        end
    ),
    awful.key( -- F1 减小屏幕亮度
        {},
        "XF86MonBrightnessDown",
        function()
            awful.util.spawn_with_shell("xbacklight -dec 5")
        end
    ),
    awful.key( -- F2 增大屏幕亮度
        {},
        "XF86MonBrightnessUp",
        function()
            awful.util.spawn_with_shell("xbacklight -inc 5")
        end
    ),
    awful.key( -- F5 减小键盘亮度
        {},
        "XF86KbdBrightnessDown",
        function()
            awful.util.spawn_with_shell("kb-light.py - 10")
        end
    ),
    awful.key( -- F6 增大键盘亮度
        {},
        "XF86KbdBrightnessUp",
        function()
            awful.util.spawn_with_shell("kb-light.py + 10")
        end
    ),
    awful.key( -- 减小音量
        {},
        "XF86AudioLowerVolume",
        function()
            awful.util.spawn_with_shell("amixer set Master 5%-")
        end
    ),
    awful.key( -- 增大音量
        {},
        "XF86AudioRaiseVolume",
        function()
            awful.util.spawn_with_shell("amixer set Master 5%+")
        end
    ),
    awful.key( -- 静音
        {},
        "XF86AudioMute",
        function()
            awful.util.spawn_with_shell("amixer set Master toggle")
        end
    )
)

-- client快捷键
clientkeys =
    gears.table.join(
    awful.key( -- mod + f 全屏
        {modkey},
        "f",
        function(c)
            c.fullscreen = not c.fullscreen
            c:raise()
        end
    ),
    awful.key( -- mod + q 关掉client
        {modkey},
        "q",
        function(c)
            c:kill()
        end
    ),
    awful.key( -- mod + ctrl + 空格 变浮动
        {modkey, "Control"},
        "space",
        awful.client.floating.toggle,
        {description = "toggle floating", group = "client"}
    ),
    awful.key( -- mod + ctrl + 回车 将当前终端与主终端对调
        {modkey, "Control"},
        "Return",
        function(c)
            c:swap(awful.client.getmaster())
        end,
        {description = "move to master", group = "client"}
    ),
    awful.key( -- mod + o 将当前client移动至另一个屏幕
        {modkey},
        "o",
        function(c)
            c:move_to_screen()
        end
    ),
    -- awful.key( -- mod + t 不懂干啥的
    --     {modkey},
    --     "t",
    --     function(c)
    --         c.ontop = not c.ontop
    --     end,
    --     {description = "toggle keep on top", group = "client"}
    -- ),
    awful.key( -- mod + n 将当前client最小化
        {modkey},
        "n",
        function(c)
            -- The client currently has the input focus, so it cannot be
            -- minimized, since minimized clients can't have the focus.
            c.minimized = true
        end,
        {description = "minimize", group = "client"}
    ),
    awful.key( -- mod + shift + n 将已经最小化的client再唤出来
        {modkey, "Shift"},
        "n",
        function()
            local c = awful.client.restore()
            -- Focus restored client
            if c then
                c:emit_signal("request::activate", "key.unminimize", {raise = true})
            end
        end,
        {description = "restore minimized", group = "client"}
    ),
    awful.key( -- mod + m 最大化当前client
        {modkey},
        "m",
        function(c)
            c.maximized = not c.maximized
            c:raise()
        end,
        {description = "(un)maximize", group = "client"}
    ),
    awful.key( -- mod + ctrl + m 垂直方向最大化
        {modkey, "Control"},
        "m",
        function(c)
            c.maximized_vertical = not c.maximized_vertical
            c:raise()
        end,
        {description = "(un)maximize vertically", group = "client"}
    ),
    awful.key( -- mod + shift + m 水平方向最大化
        {modkey, "Shift"},
        "m",
        function(c)
            c.maximized_horizontal = not c.maximized_horizontal
            c:raise()
        end,
        {description = "(un)maximize horizontally", group = "client"}
    )
)

-- 将9个数字键绑到9个tag上
for i = 1, 9 do
    globalkeys =
        gears.table.join(
        globalkeys,
        -- View tag only.
        awful.key(
            {modkey},
            "#" .. i + 9,
            function()
                local screen = awful.screen.focused()
                local tag = screen.tags[i]
                if tag then
                    tag:view_only()
                end
            end
        ),
        -- Toggle tag display.
        awful.key(
            {modkey, "Control"},
            "#" .. i + 9,
            function()
                local screen = awful.screen.focused()
                local tag = screen.tags[i]
                if tag then
                    awful.tag.viewtoggle(tag)
                end
            end
        ),
        awful.key( -- mod + shift + # 将当前client移至该tag
            {modkey, "Shift"},
            "#" .. i + 9,
            function()
                if client.focus then
                    local tag = client.focus.screen.tags[i]
                    if tag then
                        client.focus:move_to_tag(tag)
                    end
                end
            end
        ),
        -- Toggle tag on focused client.
        awful.key(
            {modkey, "Control", "Shift"},
            "#" .. i + 9,
            function()
                if client.focus then
                    local tag = client.focus.screen.tags[i]
                    if tag then
                        client.focus:toggle_tag(tag)
                    end
                end
            end
        )
    )
end

clientbuttons =
    gears.table.join(
    awful.button( -- 左键 获得焦点
        {},
        1,
        function(c)
            c:emit_signal("request::activate", "mouse_click", {raise = true})
        end
    ),
    awful.button( -- mod + 左键 移动client
        {modkey},
        1,
        function(c)
            c:emit_signal("request::activate", "mouse_click", {raise = true})
            awful.mouse.client.move(c)
        end
    ),
    awful.button( -- mod + 右键 调整client大小
        {modkey},
        3,
        function(c)
            c:emit_signal("request::activate", "mouse_click", {raise = true})
            awful.mouse.client.resize(c)
        end
    )
)

root.keys(globalkeys)

if screen.count() == 1 then
    awful.rules.rules = {
        -- 通用规则
        {
            rule = {},
            properties = {
                focus = awful.client.focus.filter,
                raise = true,
                keys = clientkeys,
                buttons = clientbuttons,
                screen = awful.screen.preferred,
                placement = awful.placement.no_overlap + awful.placement.no_offscreen,
                size_hints_honor = false -- 去掉终端间的间隙
            }
        },
        -- 浮动的应用
        {
            rule_any = {
                instance = {
                    "DTA", -- Firefox addon DownThemAll.
                    "copyq", -- Includes session name in class.
                    "pinentry"
                },
                class = {
                    "Arandr",
                    "Blueman-manager",
                    "Gpick",
                    "Kruler",
                    "MessageWin", -- kalarm.
                    "Sxiv",
                    "Tor Browser", -- Needs a fixed window size to avoid fingerprinting by screen size.
                    "Wpa_gui",
                    "veromix",
                    "xtightvncviewer",
                    "mpv", -- 播放器
                    "deepin-movie",
                    "vlc",
                    "MPlayer",
                    "smplayer",
                    "Guake",
                    "Spek",
                    "Tilda",
                    "Matplotlib", -- python画图
                    "baidunetdisk",
                    "java-lang-Thread",
                    "RStudio",
                    "mousepad",
                    "matplotlib"
                },
                -- Note that the name property shown in xprop might be set slightly after creation of the client
                -- and the name shown there might not match defined rules here.
                name = {
                    "Event Tester" -- xev.
                },
                role = {
                    "AlarmWindow", -- Thunderbird's calendar.
                    "ConfigManager", -- Thunderbird's about:config.
                    "pop-up" -- e.g. Google Chrome's (detached) Developer Tools.
                }
            },
            properties = {floating = true}
        },
        -- 第1个工作区
        {
            rule = {class = "URxvt"},
            properties = {screen = 1, tag = awful.util.tagnames[1]}
        },
        -- 第2个工作区
        {
            rule = {class = "Chromium"},
            properties = {screen = 1, tag = awful.util.tagnames[2]}
        },
        -- 第3个工作区
        {
            rule = {class = "code-oss"},
            properties = {screen = 1, tag = awful.util.tagnames[3]}
        },
        {
            rule = {class = "Code"},
            properties = {screen = 1, tag = awful.util.tagnames[3]}
        },
        {
            rule = {class = "Typora"},
            properties = {screen = 1, tag = awful.util.tagnames[3]}
        },
        -- 第4个工作区 文件管理器
        {
            rule = {class = "Pcmanfm"},
            properties = {screen = 1, tag = awful.util.tagnames[4]}
        },
        {
            rule = {class = "baidunetdisk"},
            properties = {screen = 1, tag = awful.util.tagnames[4]}
        },
        -- 第5个工作区 pdf
        {
            rule = {class = "Evince"},
            properties = {screen = 1, tag = awful.util.tagnames[5]}
        },
        {
            rule = {class = "Master PDF Editor"},
            properties = {screen = 1, tag = awful.util.tagnames[5]}
        },
        -- 第6个工作区 wps
        {
            rule = {class = "Wps"},
            properties = {screen = 1, tag = awful.util.tagnames[6]}
        },
        {
            rule = {class = "Wpp"},
            properties = {screen = 1, tag = awful.util.tagnames[6]}
        },
        {
            rule = {class = "Et"},
            properties = {screen = 1, tag = awful.util.tagnames[6]}
        },
        -- 第7个工作区 微信
        {
            rule = {class = "electronic-wechat"},
            properties = {screen = 1, tag = awful.util.tagnames[7]}
        },
        {
            rule = {class = "Qq"},
            properties = {screen = 1, tag = awful.util.tagnames[7]}
        },
        {
            rule = {class = "Wine"},
            properties = {screen = 1, tag = awful.util.tagnames[7]}
        },
        -- 第8个工作区 视频
        {
            rule = {class = "vsedit"},
            properties = {screen = 1, tag = awful.util.tagnames[8]}
        },
        {
            rule = {class = "geogebra"},
            properties = {screen = 1, tag = awful.util.tagnames[8]}
        },
        {
            rule = {class = "GNU Octave"},
            properties = {screen = 1, tag = awful.util.tagnames[8]}
        },
        {
            rule = {class = "Audacious"},
            properties = {screen = 1, tag = awful.util.tagnames[8]}
        },
        -- 第9个工作区 下载 翻墙
        {
            rule = {class = "ktorrent"},
            properties = {screen = 1, tag = awful.util.tagnames[9]}
        },
        {
            rule = {class = "transmission"},
            properties = {screen = 1, tag = awful.util.tagnames[9]}
        },
        {
            rule = {class = "shadowsocks-qt5"},
            properties = {screen = 1, tag = awful.util.tagnames[9]}
        },
        {
            rule = {class = "qv2ray"},
            properties = {screen = 1, tag = awful.util.tagnames[9]}
        }
    }
else
    awful.rules.rules = {
        -- 通用规则
        {
            rule = {},
            properties = {
                focus = awful.client.focus.filter,
                raise = true,
                keys = clientkeys,
                buttons = clientbuttons,
                screen = awful.screen.preferred,
                placement = awful.placement.no_overlap + awful.placement.no_offscreen,
                size_hints_honor = false -- 去掉终端间的间隙
            }
        },
        -- 浮动的应用
        {
            rule_any = {
                instance = {
                    "DTA", -- Firefox addon DownThemAll.
                    "copyq", -- Includes session name in class.
                    "pinentry"
                },
                class = {
                    "Arandr",
                    "Blueman-manager",
                    "Gpick",
                    "Kruler",
                    "MessageWin", -- kalarm.
                    "Sxiv",
                    "Tor Browser", -- Needs a fixed window size to avoid fingerprinting by screen size.
                    "Wpa_gui",
                    "veromix",
                    "xtightvncviewer",
                    "mpv", -- 播放器
                    "deepin-movie",
                    "vlc",
                    "MPlayer",
                    "smplayer",
                    "Guake",
                    "Spek",
                    "Tilda",
                    "Matplotlib", -- python画图
                    "baidunetdisk",
                    "java-lang-Thread",
                    "RStudio",
                    "mousepad",
                    "matplotlib"
                },
                -- Note that the name property shown in xprop might be set slightly after creation of the client
                -- and the name shown there might not match defined rules here.
                name = {
                    "Event Tester" -- xev.
                },
                role = {
                    "AlarmWindow", -- Thunderbird's calendar.
                    "ConfigManager", -- Thunderbird's about:config.
                    "pop-up" -- e.g. Google Chrome's (detached) Developer Tools.
                }
            },
            properties = {floating = true}
        },
        -- 第1个工作区
        {
            rule = {class = "URxvt"},
            properties = {screen = 1, tag = awful.util.tagnames[1]}
        },
        -- 第2个工作区
        {
            rule = {class = "Chromium"},
            properties = {screen = 1, tag = awful.util.tagnames[2]}
        },
        -- 第3个工作区
        {
            rule = {class = "code-oss"},
            properties = {screen = 1, tag = awful.util.tagnames[3]}
        },
        {
            rule = {class = "Code"},
            properties = {screen = 1, tag = awful.util.tagnames[3]}
        },
        -- 第4个工作区 文件管理器
        {
            rule = {class = "Pcmanfm"},
            properties = {screen = 1, tag = awful.util.tagnames[4]}
        },
        {
            rule = {class = "baidunetdisk"},
            properties = {screen = 1, tag = awful.util.tagnames[4]}
        },
        -- 第5个工作区 pdf
        {
            rule = {class = "Evince"},
            properties = {screen = 1, tag = awful.util.tagnames[5]}
        },
        {
            rule = {class = "Master PDF Editor"},
            properties = {screen = 1, tag = awful.util.tagnames[5]}
        },
        -- 第6个工作区 wps
        {
            rule = {class = "Wps"},
            properties = {screen = 1, tag = awful.util.tagnames[6]}
        },
        {
            rule = {class = "Wpp"},
            properties = {screen = 1, tag = awful.util.tagnames[6]}
        },
        {
            rule = {class = "Et"},
            properties = {screen = 1, tag = awful.util.tagnames[6]}
        },
        {
            rule = {class = "Typora"},
            properties = {screen = 1, tag = awful.util.tagnames[6]}
        },
        -- 第7个工作区 微信
        {
            rule = {class = "electronic-wechat"},
            properties = {screen = 2, tag = awful.util.tagnames[7]}
        },
        {
            rule = {class = "Qq"},
            properties = {screen = 1, tag = awful.util.tagnames[7]}
        },
        {
            rule = {class = "Wine"},
            properties = {screen = 1, tag = awful.util.tagnames[7]}
        },
        -- 第8个工作区 视频
        {
            rule = {class = "vsedit"},
            properties = {screen = 1, tag = awful.util.tagnames[8]}
        },
        {
            rule = {class = "geogebra"},
            properties = {screen = 1, tag = awful.util.tagnames[8]}
        },
        {
            rule = {class = "GNU Octave"},
            properties = {screen = 1, tag = awful.util.tagnames[8]}
        },
        {
            rule = {class = "Audacious"},
            properties = {screen = 1, tag = awful.util.tagnames[8]}
        },
        -- 第9个工作区 下载 翻墙
        {
            rule = {class = "ktorrent"},
            properties = {screen = 1, tag = awful.util.tagnames[9]}
        },
        {
            rule = {class = "transmission"},
            properties = {screen = 1, tag = awful.util.tagnames[9]}
        },
        {
            rule = {class = "shadowsocks-qt5"},
            properties = {screen = 1, tag = awful.util.tagnames[9]}
        },
        {
            rule = {class = "qv2ray"},
            properties = {screen = 1, tag = awful.util.tagnames[9]}
        }
    }
end

-- Signal function to execute when a new client appears.
client.connect_signal(
    "manage",
    function(c)
        -- Set the windows at the slave, i.e. put it at the end of others instead of setting it master.
        -- if not awesome.startup then awful.client.setslave(c) end
        if awesome.startup and not c.size_hints.user_position and not c.size_hints.program_position then
            -- Prevent clients from being unreachable after screen count changes.
            awful.placement.no_offscreen(c)
        end
    end
)

-- 焦点跟随鼠标
client.connect_signal(
    "mouse::enter",
    function(c)
        c:emit_signal("request::activate", "mouse_enter", {raise = false})
    end
)

-- 跟随awesome一起启动的脚本
awful.spawn.with_shell("~/.config/awesome/autorun.sh")
